<?php

/**
 * company actions.
 *
 * @package    maximous
 * @subpackage company
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class companyActions extends MaxFrontendActions
{

    public function preExecute()
    {
        parent::preExecute();
        $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Company);
    }

    public function executeIndex(sfWebRequest $request)
    {
        $page        = $request->getParameter('page');
        $query       = CompanyTable::getInstance()->getUsersCompanys();
        $this->pager = $this->createPager('Company', $query, $page, sfConfig::get('app_home_companys_per_page'));
    }

    public function executeAddCompany(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canAdd(), $this->i18n->__("you dont't have permission to access this page"));

        // check if user aready have company
        $company           = CompanyTable::getInstance()->findOneByUserId($this->maxUser->getUserDataByIndex('id'));
            $this->forward404If($company, $this->i18n->__('You can only add one company'));
        $this->companyForm = new FrontendCompanyForm(null, array('maxUser' => $this->maxUser, 'i18n'    => $this->i18n));
        if ($request->isMethod(sfWebRequest::POST))
        {
            $companyFormData = $request->getParameter($this->companyForm->getName());
            $companyFormFiles = $request->getFiles($this->companyForm->getName());
            $this->companyForm->bind($companyFormData,$companyFormFiles);
            if ($this->companyForm->isValid())
            {
                $this->companyForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('add  company successful'));
            }
        }
    }

    public function executeEditCompany(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canEdit(), $this->i18n->__("you dont't have permission to access this page"));
        $company           = CompanyTable::getInstance()->findOneByUserId($this->maxUser->getUserDataByIndex('id'));
        $this->companyForm = new FrontendCompanyForm($company, array('maxUser' => $this->maxUser, 'i18n'    => $this->i18n));
        if ($request->isMethod(sfWebRequest::POST))
        {
            $companyFormData = $request->getParameter($this->companyForm->getName());
            $this->companyForm->bind($companyFormData);
            if ($this->companyForm->isValid())
            {
                $this->companyForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('edit company successful'));
            }
        }
    }

    public function executeDeleteCompany(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canDelete(), $this->i18n->__("you dont't have permission to access this page"));
        $company = CompanyTable::getInstance()->findOneByUserId($this->maxUser->getUserDataByIndex('id'));
        // delete company object
        $company->delete();
        $this->getUser()->setFlash('message_success', $this->i18n->__('delete company successful'));
        $this->redirect('/');
    }

    public function executeShowCompany(sfWebRequest $request)
    {
        $watchedCompany = $this->getUser()->getAttribute('watched_company', array());
        $companyTitle = $request->getParameter('jtitle');
        $company      = CompanyTable::getInstance()->findOneByTitle($companyTitle);
        if (!in_array($company->getId(), $watchedCompany))
        {
            $company->increamentVisits();
            $company->save();
            $watchedCompany[] = $company->getId();
            $this->getUser()->setAttribute('watched_company', $watchedCompany);
        }
        $this->forward404Unless($company);
        $this->company    = $company;
    }

}